package com.youyi.yychosesdk.utils.bitmap;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import com.youyi.yychosesdk.EasyPhotos;
import com.youyi.yychosesdk.models.album.entity.Photo;
import com.youyi.yychosesdk.utils.uri.UriUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class BitmapUtils {
    public static void recycle(Bitmap bitmap) {
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
        System.gc();
    }

    public static void recycle(Bitmap... bitmapArr) {
        for (Bitmap bitmap : bitmapArr) {
            recycle(bitmap);
        }
    }

    public static void recycle(List<Bitmap> list) {
        for (Bitmap bitmap : list) {
            recycle(bitmap);
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:8:0x0016, code lost:
        if (r15 != 270) goto L_0x0025;
     */
    /* JADX WARNING: Removed duplicated region for block: B:13:0x0036  */
    /* JADX WARNING: Removed duplicated region for block: B:14:0x0039  */
    /* JADX WARNING: Removed duplicated region for block: B:19:0x0062  */
    /* JADX WARNING: Removed duplicated region for block: B:20:0x006a  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static Bitmap addWatermark(Bitmap r9, Bitmap r10, int r11, int r12, int r13, boolean r14, int r15) {
        /*
        // Method dump skipped, instructions count: 127
        */
        throw new UnsupportedOperationException("Method not decompiled: com.youyi.yychosesdk.utils.bitmap.BitmapUtils.addWatermark(android.graphics.Bitmap, android.graphics.Bitmap, int, int, int, boolean, int):android.graphics.Bitmap");
    }

    /* JADX WARNING: Code restructure failed: missing block: B:9:0x0027, code lost:
        if (r24 != 270) goto L_0x0038;
     */
    /* JADX WARNING: Removed duplicated region for block: B:14:0x004d  */
    /* JADX WARNING: Removed duplicated region for block: B:15:0x0050  */
    /* JADX WARNING: Removed duplicated region for block: B:20:0x0097  */
    /* JADX WARNING: Removed duplicated region for block: B:21:0x00aa  */
    /* JADX WARNING: Removed duplicated region for block: B:24:0x00d3  */
    /* JADX WARNING: Removed duplicated region for block: B:25:0x00e2  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static Bitmap addWatermarkWithText(Bitmap r17, Bitmap r18, int r19, String r20, int r21, int r22, boolean r23, int r24) {
        /*
        // Method dump skipped, instructions count: 261
        */
        throw new UnsupportedOperationException("Method not decompiled: com.youyi.yychosesdk.utils.bitmap.BitmapUtils.addWatermarkWithText(android.graphics.Bitmap, android.graphics.Bitmap, int, java.lang.String, int, int, boolean, int):android.graphics.Bitmap");
    }

    public static void saveBitmapToDir(final Activity activity, final String str, final String str2, final Bitmap bitmap, final boolean z, final SaveBitmapCallBack saveBitmapCallBack) {
        new Thread(new Runnable() {
            /* class com.youyi.yychosesdk.utils.bitmap.BitmapUtils.AnonymousClass1 */

            public void run() {
                if (Build.VERSION.SDK_INT > 28) {
                    BitmapUtils.saveBitmapToDirQ(activity, str, str2, bitmap, z, saveBitmapCallBack);
                    return;
                }
                File file = new File(str);
                if ((!file.exists() || !file.isDirectory()) && !file.mkdirs()) {
                    activity.runOnUiThread(new Runnable() {
                        /* class com.youyi.yychosesdk.utils.bitmap.BitmapUtils.AnonymousClass1.AnonymousClass1 */

                        public void run() {
                            saveBitmapCallBack.onCreateDirFailed();
                        }
                    });
                    return;
                }
                try {
                    final File createTempFile = File.createTempFile(str2, ".png", file);
                    FileOutputStream fileOutputStream = new FileOutputStream(createTempFile);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    if (z) {
                        EasyPhotos.notifyMedia(activity, createTempFile);
                    }
                    activity.runOnUiThread(new Runnable() {
                        /* class com.youyi.yychosesdk.utils.bitmap.BitmapUtils.AnonymousClass1.AnonymousClass2 */

                        public void run() {
                            saveBitmapCallBack.onSuccess(createTempFile);
                        }
                    });
                } catch (IOException e) {
                    activity.runOnUiThread(new Runnable() {
                        /* class com.youyi.yychosesdk.utils.bitmap.BitmapUtils.AnonymousClass1.AnonymousClass3 */

                        public void run() {
                            saveBitmapCallBack.onIOFailed(e);
                        }
                    });
                }
            }
        }).start();
    }

    /* access modifiers changed from: private */
    public static void saveBitmapToDirQ(final Activity activity, String str, String str2, Bitmap bitmap, boolean z, final SaveBitmapCallBack saveBitmapCallBack) {
        Uri uri;
        ContentValues contentValues = new ContentValues();
        contentValues.put("_display_name", str2 + System.currentTimeMillis() + ".png");
        contentValues.put("mime_type", "image/png");
        int lastIndexOf = str.lastIndexOf("/");
        if (lastIndexOf == str.length()) {
            lastIndexOf = str.substring(0, lastIndexOf - 1).lastIndexOf("/");
        }
        contentValues.put("relative_path", "DCIM/" + str.substring(lastIndexOf + 1));
        ContentResolver contentResolver = activity.getContentResolver();
        if (Environment.getExternalStorageState().equals("mounted")) {
            uri = MediaStore.Images.Media.getContentUri("external");
        } else {
            uri = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
        }
        final Uri insert = contentResolver.insert(uri, contentValues);
        if (insert == null) {
            activity.runOnUiThread(new Runnable() {
                /* class com.youyi.yychosesdk.utils.bitmap.BitmapUtils.AnonymousClass2 */

                public void run() {
                    saveBitmapCallBack.onCreateDirFailed();
                }
            });
            return;
        }
        try {
            OutputStream openOutputStream = contentResolver.openOutputStream(insert);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, openOutputStream);
            if (openOutputStream != null) {
                openOutputStream.flush();
                openOutputStream.close();
            }
            activity.runOnUiThread(new Runnable() {
                /* class com.youyi.yychosesdk.utils.bitmap.BitmapUtils.AnonymousClass3 */

                public void run() {
                    String pathByUri = UriUtils.getPathByUri(activity, insert);
                    if (pathByUri == null) {
                        saveBitmapCallBack.onCreateDirFailed();
                    } else {
                        saveBitmapCallBack.onSuccess(new File(pathByUri));
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
            activity.runOnUiThread(new Runnable() {
                /* class com.youyi.yychosesdk.utils.bitmap.BitmapUtils.AnonymousClass4 */

                public void run() {
                    saveBitmapCallBack.onIOFailed(e);
                }
            });
        }
    }

    public static Bitmap createBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        view.draw(new Canvas(createBitmap));
        return createBitmap;
    }

    public static Boolean needChangeWidthAndHeight(Photo photo) throws IOException {
        boolean z = true;
        int attributeInt = new ExifInterface(photo.path).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 1);
        if (!(attributeInt == 6 || attributeInt == 8)) {
            z = false;
        }
        return Boolean.valueOf(z);
    }

    public static void calculateLocalImageSizeThroughBitmapOptions(Photo photo) throws IOException {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(photo.path, options);
        photo.width = options.outWidth;
        photo.height = options.outHeight;
    }

    public static Bitmap adjustBitmapRotation(Bitmap bitmap, int i) {
        float f;
        float f2;
        Matrix matrix = new Matrix();
        matrix.setRotate((float) i, ((float) bitmap.getWidth()) / 2.0f, ((float) bitmap.getHeight()) / 2.0f);
        if (i == 90) {
            f = (float) bitmap.getHeight();
            f2 = 0.0f;
        } else {
            f = (float) bitmap.getHeight();
            f2 = (float) bitmap.getWidth();
        }
        float[] fArr = new float[9];
        matrix.getValues(fArr);
        matrix.postTranslate(f - fArr[2], f2 - fArr[5]);
        Bitmap createBitmap = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
        new Canvas(createBitmap).drawBitmap(bitmap, matrix, new Paint());
        return createBitmap;
    }
}
